package com.icss.ah.coupons.action;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.coupons.service.CouponsService;

@Controller
public class CouponsAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private final Logger errlog = Logger.getLogger("E");
	
	@Autowired
	private CouponsService couponsService;
	
	private String cpsId; //优惠卷id
	private String cpsName;
	private String useDate;//生效时间
	private String facePrice;//面额
	private String cpsAmount;//发行量
	private String lmtAmount;//每人限量
	private String begDate;
	private String endDate;
	private String useCtn;//使用条件
	private String status;
	private String outType;//发放方式
	
	public String toCouponsListPage(){
		return "toCouponsListPage";
	}
	
	public void queryCouponsList(){
		try {
			Map<String, String> map = new HashMap<String, String>();
			String corpId = getBaseCorpId(); //从配置文件里取公众号id
			String userCode = getUserCodeFromSession();
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("cpsName", cpsName);
			map.put("status", status);
			map.put("useDate", useDate);
			map.put("outType", outType);
			map.put("startIndex", getStartIndexMysql());
			map.put("endIndex", getEndIndexMysql());
			List<Map<String, Object>> list = couponsService.getCouponsList(map);
			Long total = couponsService.getCouponsListCount(map);
			dataGridJson = new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CouponsAction|异常：",e);
		} finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
	}
	
	public String couponsPage(){
		if(null!=cpsId&&!"".equals(cpsId)){
			Map<String, Object> info = couponsService.getCouponsInfo(cpsId);
			int fp = ((BigDecimal) info.get("facePrice")).intValue();
			info.put("facePrice", fp);
			int uc = ((BigDecimal) info.get("useCtn")).intValue();
			info.put("useCtn", uc);
			request = getRequest();
			request.setAttribute("info", info);
			request.setAttribute("flag", request.getParameter("flag"));
		}
		return "couponsPage";
	}
	
	public void save(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			String corpId = getBaseCorpId(); //从配置文件里取公众号id
			String userCode = getUserCodeFromSession();
			map.put("cpsId", UUID.randomUUID().toString());
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("cpsName", cpsName);
			map.put("cpsType", "1");
			map.put("facePrice", facePrice);
			map.put("cpsAmount", cpsAmount);
			map.put("lmtAmount", lmtAmount);
			map.put("begDate", begDate);
			map.put("endDate", endDate);
			map.put("useCtn", useCtn);
			map.put("status", status);
			map.put("outType", outType);
			couponsService.saveInfo(map);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("CouponsAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	public void updateInfo(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("cpsId", cpsId);
			map.put("cpsName", cpsName);
			map.put("facePrice", facePrice);
			map.put("cpsAmount", cpsAmount);
			map.put("lmtAmount", lmtAmount);
			map.put("begDate", begDate);
			map.put("endDate", endDate);
			map.put("useCtn", useCtn);
			map.put("status", status);
			map.put("outType", outType);
			couponsService.updateInfo(map);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("CouponsAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	public void updateStatus(){
		JSONObject json = new JSONObject();
		try {
			couponsService.updateStatus(cpsId);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("CouponsAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}

	/**
	 * 优惠卷删除
	 * @param cpsId
	 */
	public void deleteInfo(){
		jsonText = new Json();
		try{
			HashMap map = new HashMap();
			map.put("cpsId", cpsId);
			boolean result = couponsService.deleteInfo(map);
			jsonText.setSuccess(result);
		} catch (Exception e) {
			errlog.error("CouponsAction|deleteInfo|异常：",e);
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	public String getCpsId() {
		return cpsId;
	}

	public void setCpsId(String cpsId) {
		this.cpsId = cpsId;
	}

	public String getCpsName() {
		return cpsName;
	}

	public void setCpsName(String cpsName) {
		this.cpsName = cpsName;
	}

	public String getUseDate() {
		return useDate;
	}

	public void setUseDate(String useDate) {
		this.useDate = useDate;
	}

	public String getFacePrice() {
		return facePrice;
	}

	public void setFacePrice(String facePrice) {
		this.facePrice = facePrice;
	}

	public String getCpsAmount() {
		return cpsAmount;
	}

	public void setCpsAmount(String cpsAmount) {
		this.cpsAmount = cpsAmount;
	}

	public String getLmtAmount() {
		return lmtAmount;
	}

	public void setLmtAmount(String lmtAmount) {
		this.lmtAmount = lmtAmount;
	}

	public String getBegDate() {
		return begDate;
	}

	public void setBegDate(String begDate) {
		this.begDate = begDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getUseCtn() {
		return useCtn;
	}

	public void setUseCtn(String useCtn) {
		this.useCtn = useCtn;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getOutType() {
		return outType;
	}

	public void setOutType(String outType) {
		this.outType = outType;
	}


}
